package pac;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.sql.Date;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.dom4j.Document;

import pac.LoginTest.MyParseHandler;

public class RegisterTest {

	private static final String SERVER = "http://222.200.180.9/PaperPlaneServer/";
//	private static final String SERVER = "http://127.0.0.1:8080/PaperPlaneServer/";
	
	public static void main(String[] args) {
		try {
			URL url = new URL( SERVER + "register");
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
			
//		设置请求参数
			connection.setReadTimeout(20000);
			connection.setRequestMethod("POST");
			connection.setDoOutput(true);
			connection.setDoInput(true);

			connection.connect();
			
			PrintWriter writer = new PrintWriter( connection.getOutputStream() );
			
			Date date = new Date( System.currentTimeMillis());
			Document document = XMLUtils.changeToXML("register", new String[]{XMLUtils.TAG_EMAIL, XMLUtils.TAG_PASSWORD, XMLUtils.TAG_USERNAME, XMLUtils.TAG_GENDER, XMLUtils.TAG_BIRTHDAY }, 
					new String[]{ "test2@gmail.com", "123456", "test_user", ""+1, date.getTime()+"" });
			document.write( writer);
			writer.flush();
			if( !connection.getHeaderField( XMLUtils.STATUSCODE).equals("200")){
				System.out.println( "User Exist!");
				return ;
			}
				
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();
			MyParseHandler handler = new MyParseHandler();
			parser.parse(connection.getInputStream(),  handler );
			int userID = handler.getUserID();
			System.out.println("Finished UserID: " + userID);
		
//			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
//		
	}

}
